package com.sgb.packers.widget;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.netease.biz_video_group.yunxin.voideoGroup.util.VibrateUtil;
import com.sgb.link.view.adapter.YoungAdapterOnItemClick;
import com.sgb.packers.R;
import com.sgb.packers.view.adapter.TimeSalaryAdapter;
import com.swgk.core.dialog.BaseBottomDialog;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogViewHolder;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.interfaces.DialogClick;
import com.zjkj.qgbui.widgets.inputtext.DecimalInputTextWatcher;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author : younghow
 * @date : 2021/11/25 17:37
 * description:
 */
public class InputWorkDayPopWindow extends BaseBottomDialog implements View.OnClickListener {

    private DialogClick mDialogClick;
    private TimeSalaryAdapter timeSalaryAdapter;
    private EditText tvSelectSalaryNumber;

    public InputWorkDayPopWindow(Context context, DialogCallBack dialogCallBack) {
        super(context, dialogCallBack);
    }

    public void setDialogClick(DialogClick mDialogClick) {
        this.mDialogClick = mDialogClick;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_pop_sure){
            dismiss();
            if (timeSalaryAdapter.getCheckDatas().size() > 0){
                mDialogClick.confirmClick(timeSalaryAdapter.getCheckDatas().get(0).replace("个工",""),null);
            }else {
                if (!TextUtils.isEmpty(tvSelectSalaryNumber.getText().toString())){
                    mDialogClick.confirmClick(tvSelectSalaryNumber.getText().toString(),null);
                }else {
                    mDialogClick.confirmClick("",null);
                }
            }
        } else if (v.getId() == R.id.tv_pop_cancel){
            mDialogClick.cancelClick();
            dismiss();
        }
    }

    @Override
    protected int dialogLayout() {
        return R.layout.packers_pop_input_work_day;
    }

    @Override
    protected void initView(DialogViewHolder holder, Bundle savedInstanceState) {
        TextView tvPopSure = holder.getDialog().findViewById(R.id.tv_pop_sure);
        TextView tvPopCancel = holder.getDialog().findViewById(R.id.tv_pop_cancel);
        RecyclerView rvTimeSalary = holder.getDialog().findViewById(R.id.rv_time_salary);
        tvSelectSalaryNumber = holder.getDialog().findViewById(R.id.tv_select_salary_number);

        tvPopCancel.setOnClickListener(this);
        tvPopSure.setOnClickListener(this);

        rvTimeSalary.setLayoutManager(new GridLayoutManager(context,4));
        List<String> time = new ArrayList<>();
        time.add("0.2个工");
        time.add("0.5个工");
        time.add("0.8个工");
        time.add("1.5个工");
        time.add("1.8个工");
        time.add("2个工");
        timeSalaryAdapter = new TimeSalaryAdapter(context,time);
        timeSalaryAdapter.setItemSelect(true);
        rvTimeSalary.setAdapter(timeSalaryAdapter);
        timeSalaryAdapter.setOnItemClick(new YoungAdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                if (timeSalaryAdapter.getCheckDatas().contains(s)){
                    tvSelectSalaryNumber.setText("");
                }
            }
        });

        tvSelectSalaryNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(tvSelectSalaryNumber.getText().toString())){
                    timeSalaryAdapter.getCheckDatas().clear();
                    timeSalaryAdapter.setDefItem(-1);
                }
            }
        });

        tvSelectSalaryNumber.addTextChangedListener(new DecimalInputTextWatcher(tvSelectSalaryNumber,2,1));
    }

    public void setDefault(double number){
        tvSelectSalaryNumber.setText(String.format(Locale.CHINA,"%.1f",number));
    }
}
